﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Threading;
using System.IO;

namespace ClientPerformance
{                                                
    interface IPeerPerformanceChannel : IPeerServiceContract, IClientChannel {}

    internal delegate void UIUpdateDelegate();
    internal delegate void UpdateStatus(string status);
    internal delegate void UpdateTrafficPattern(TrafficPatternInfo info);

    public partial class PeerView : Form
    {
        private string nodeName;
        private TrafficPatternInfo pattern;

        public PeerView()
        {
            InitializeComponent();
            Startup(Guid.NewGuid().ToString());
        }
        
        public PeerView(string name)
        {
            InitializeComponent();
            Startup(name);
        }

        private void Startup(string name)
        {
            // Load the default settings for the Traffic Pattern
            pattern = new TrafficPatternInfo
            {
                PacketSize = 1024,
                SendRateInMilliseconds = 1000,
                MaxMessages = 2400,   /* 1 Message per Second for 40 Minutes   */
                RampUpRateInterval = 1000,
                StartUpRate = 1000
            };

            trafficPatternInfoBindingSource.DataSource =  pattern ;

            uxNodeName.Text = nodeName = name;

            Thread workerThread = new Thread(WorkerThread.Run);
            WorkerThread.StatusUpdate += new UpdateStatusDelegate(WorkerThread_StatusUpdate);
            WorkerThread.TrafficPatternUpdated += new TrafficPatternUpdateDelegate(WorkerThread_TrafficPatternUpdated);
            WorkerThread.StartSendingTraffic += new FireEventDelegate(WorkerThread_StartSendingTraffic);
            WorkerThread.StopSendingTraffic += new FireEventDelegate(WorkerThread_StopSendingTraffic);
            workerThread.Start(nodeName);
        }


        #region WorkerThread Events
        void WorkerThread_StopSendingTraffic()
        {
            if (InvokeRequired)
                BeginInvoke(new UIUpdateDelegate(UpdateViewStopped));
            else
                UpdateViewStopped();
        }

        void WorkerThread_StartSendingTraffic()
        {
            if (InvokeRequired)
                BeginInvoke(new UIUpdateDelegate(UpdateViewSending));
            else
                UpdateViewSending();
        }

        void WorkerThread_TrafficPatternUpdated(TrafficPatternInfo info)
        {
            if (InvokeRequired)
                BeginInvoke(new UpdateTrafficPattern(UpdateTrafficPatternView), info);
            else
                UpdateTrafficPatternView(info);
        }

        void WorkerThread_StatusUpdate(string status)
        {
            if (InvokeRequired)
                BeginInvoke(new UpdateStatus(UpdateViewStatus), status);
            else
                UpdateViewStatus(status);
        }
        #endregion

        private void UpdateTrafficPatternView(TrafficPatternInfo info)
        {
            trafficPatternInfoBindingSource.DataSource = info;
        }

        private void UpdateViewStopped()
        {
            uxClientState.BackColor = Color.Red;
            uxClientState.Text = "STOPPED";
        }

        private void UpdateViewSending()
        {
            uxClientState.BackColor = Color.Green;
            uxClientState.Text = "SENDING";
        }

        private void UpdateViewStatus(string status)
        {
            uxStatus.Text = status;
        }

        private void PeerView_FormClosing(object sender, FormClosingEventArgs e)
        {
            WorkerThread.Shutdown();
        }

        private void uxStop_Click(object sender, EventArgs e)
        {
            WorkerThread.StopTraffic( /*localOnly*/ false);
        }

        private void uxStart_Click(object sender, EventArgs e)
        {
            TrafficPatternInfo info = trafficPatternInfoBindingSource.DataSource as TrafficPatternInfo;

            WorkerThread.UpdateTrafficPattern(info);
            Thread.Sleep(1000);
            WorkerThread.StartTraffic(uxLocalOnly.Checked);
        }

        private void uxPatternSelected(object sender, EventArgs e)
        {
            if (uxRate1.Checked)
            {
                pattern.SendRateInMilliseconds = 1000;
                pattern.MaxMessages = 900;
            }
            else if (uxRate2.Checked)
            {
                pattern.SendRateInMilliseconds = 500;
                pattern.MaxMessages = 1800;
            }
            else if (uxRate4.Checked)
            {
                pattern.SendRateInMilliseconds = 250;
                pattern.MaxMessages = 3600;
            }
            else if (uxRate8.Checked)
            {
                pattern.SendRateInMilliseconds = 125;
                pattern.MaxMessages = 7200;
            }

            trafficPatternInfoBindingSource.ResetCurrentItem();
        }      
    }
}
